import scrapy

from ..items import StockItem
import  csv

class XueqiuSpider(scrapy.Spider):
    name = "xueqiu"
    allowed_domains = ["xueqiu.com"]
    start_url = "https://stock.xueqiu.com/v5/stock/screener/quote/list.json?page={}&size=30&order=desc&orderby=percent&order_by=percent&market=CN&type=sh_sz&ind_code={}"

    headers = {
        'authority': 'stock.xueqiu.com',
        'accept': 'application/json, text/plain, */*',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'cache-control': 'no-cache',
        # 'cookie': 'device_id=9223a30a2c922298b44af998b4b64917; s=cl12ekkjq4; Hm_lvt_1db88642e346389874251b5a1eded6e3=1676799206,1677379617,1677834970; u=811683872781183; xq_a_token=ebd3592a6f07cb960b9cb980e00fbb4dce12da70; xq_r_token=f8a19d1fe4aeeb1605c79b4c4037ed3110905745; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY4NjQ0MzQ1NCwiY3RtIjoxNjgzODc0Mjc5OTQwLCJjaWQiOiJkOWQwbjRBWnVwIn0.Xmlvuk7nlHdfb2etb4_ccdoE2PtzizNJG013tiNooQy2q8C108s6ILbnK5yVT0mLvlMOXAK0N9_O8ebXZxU4sFqvPq4WQwAz6UVRsCBRGGsExQuy_-v94ShKyL7niCWQ02_aQT58i-SX79sg3MHR0PjzZ7j6wJdhT9-sc6gu4LZL8IDID00dy70rS0S1vedHrUlZU9MAYg2wYquL5fAXWY6LfDUWdX_2UDfQyzXfuTjX95Dh7Oq9yZYEhlWpLtPki0swOwlljYH83XR4eoYyttub0JbEfhvuMkoa0tXkklbyMkfit0BAfa_eRlJzZ713rAendgpVX-bp1v5rk2L6Vw',
        'origin': 'https://xueqiu.com',
        'pragma': 'no-cache',
        'referer': 'https://xueqiu.com/hq',
        'sec-ch-ua': '"Microsoft Edge";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35',
    }

    cookies = {
        'device_id': '9223a30a2c922298b44af998b4b64917',
        's': 'cl12ekkjq4',
        'Hm_lvt_1db88642e346389874251b5a1eded6e3': '1676799206,1677379617,1677834970',
        'u': '811683872781183',
        'xq_a_token': 'ebd3592a6f07cb960b9cb980e00fbb4dce12da70',
        'xq_r_token': 'f8a19d1fe4aeeb1605c79b4c4037ed3110905745',
        'xq_id_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOi0xLCJpc3MiOiJ1YyIsImV4cCI6MTY4NjQ0MzQ1NCwiY3RtIjoxNjgzODc0Mjc5OTQwLCJjaWQiOiJkOWQwbjRBWnVwIn0.Xmlvuk7nlHdfb2etb4_ccdoE2PtzizNJG013tiNooQy2q8C108s6ILbnK5yVT0mLvlMOXAK0N9_O8ebXZxU4sFqvPq4WQwAz6UVRsCBRGGsExQuy_-v94ShKyL7niCWQ02_aQT58i-SX79sg3MHR0PjzZ7j6wJdhT9-sc6gu4LZL8IDID00dy70rS0S1vedHrUlZU9MAYg2wYquL5fAXWY6LfDUWdX_2UDfQyzXfuTjX95Dh7Oq9yZYEhlWpLtPki0swOwlljYH83XR4eoYyttub0JbEfhvuMkoa0tXkklbyMkfit0BAfa_eRlJzZ713rAendgpVX-bp1v5rk2L6Vw',
    }

    def start_requests(self):
        with open('stocks.csv', 'r', encoding='utf-8') as f:
            reader = csv.DictReader(f)
            for row in reader:
                Ccode= row['Ccode']
                Cname= row['Cname']
                for page in range(1, 5):
                    url = self.start_url.format(page, Ccode)
                    yield scrapy.Request(url=url, cookies=self.cookies, headers=self.headers, callback=self.parse,  meta= {'Ccode': Ccode, 'Cname': Cname})

    def parse(self, response):
        data = response.json()
        Ccode=response.meta['Ccode']
        Cname=response.meta['Cname']
        items = data.get('data').get('list')
        for item in items:
            stock_item = StockItem()
            stock_item['Ccode'] = Ccode
            stock_item['Cname'] = Cname
            stock_item['symbol'] = item.get('symbol')
            stock_item['name'] = item.get('name')
            stock_item['current'] = item.get('current')
            stock_item['chg'] = item.get('chg')
            stock_item['percent'] = item.get('percent')
            stock_item['current_year_percent'] = item.get('current_year_percent')
            stock_item['volume'] = item.get('volume')
            stock_item['amount'] = item.get('amount')
            stock_item['turnover_rate'] = item.get('turnover_rate')
            stock_item['dividend_yield'] = item.get('dividend_yield')
            stock_item['market_capital'] = item.get('market_capital')
            yield stock_item

